﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilLib.IO
{
	public static  class StreamEx
	{
		/// <summary>
		/// 读取所有文本，读完后关闭Stream
		/// </summary>
		public static IEnumerable<string> ReadLines(this Stream stream, Encoding encoding)
		{
			using (stream)
			using (var reader = new StreamReader(stream, encoding))
			{
				string line = null;
				while ((line = reader.ReadLine()) != null)
				{
					yield return line;
				}
			}
		}

		/// <summary>
		/// 读取所有文本，读完后关闭Stream
		/// </summary>
		public static string ReadAllText(this Stream stream, Encoding encoding)
		{
			using (stream)
			using (var reader = new StreamReader(stream, encoding))
			{
				return reader.ReadToEnd();
			}
		}

		/// <summary>
		/// 读取所有内容，读完后关闭Stream
		/// </summary>
		/// <param name="stream"></param>
		/// <returns></returns>
		public static byte[] ReadAllBytes(this Stream stream)
		{
			using (stream)
			using (var memory = new MemoryStream())
			{
				stream.CopyTo(memory);
				return memory.ToArray();
			}
		}

		//TODO 实现一个带进度报告的copy to
		public static void CopyTo(this Stream stream, IProgress<int> progress)
		{
			throw new NotImplementedException();
		}
	}
}
